
# --- Libraries --- #
library(countrycode)
set.seed(1912)

setwd("YOUR WORKING DIRECTORY")



# --- Functions for WTI cleaning:

fix.WTI <- function(x){
  
  colnames(x) <- c("treaty_id", "laterality", "parties", "date", "headnote") # change colnames
  
  x$parties[x$parties == "Armenia (See USSR Before 1991)"] <- "Armenia"
  x$parties[x$parties == "Azerbaijan (See USSR Before 1991)"] <- "Azerbaijan"
  x$parties[x$parties == "Czechoslovakia ( use for Czechoslovak Socialist Republic)"] <- "Czechoslovakia"
  x$parties[x$parties == "Estonia (See USSR Before 1991)"] <- "Estonia"
  x$parties[x$parties == "Eritrea (See Ethiopia Before 5/93)"] <- "Eritrea"
  x$parties[x$parties == "Georgia (See USSR Before 1991)"] <- "Georgia"
  x$parties[x$parties == "Germany, East (German Democratic Republic)"] <- "German Democratic Republic"
  x$parties[x$parties == "Latvia (See USSR Before 1991)"] <- "Latvia"
  x$parties[x$parties == "Lithuania (See USSR Before 1991)"] <- "Lithuania"
  x$parties[x$parties == "Moldova (See USSR Before 1991)"] <- "Moldova"
  x$parties[x$parties == "Turkmenistan (See USSR Before 1991)"] <- "Turkmenistan"
  x$parties[x$parties == "Yemen, Peoples Democratic Republic of (South)"] <- "South Yemen"
  x$parties[x$parties == "Yugoslavia, Socialist Federal Republic of"] <- "Yugoslavia"
  x$parties[x$parties == "Union of Soviet Socialist Republics (See individual republics after 1991)"] <- "Union of Soviet Socialist Republics"
  x$parties[x$parties == "Uzbekistan (See USSR Before 1991)"] <- "Uzbekistan"
  
  x_df <- x[rowSums(is.na(x)) == 4,]
  
  party2 <- x_df$parties
  
  df <- data.frame(state1 = na.omit(x$parties[c(TRUE,FALSE)]), 
                   state2 = party2, 
                   date = na.omit(x$date), 
                   note = na.omit(x$headnote),
                   long1 = na.omit(x$parties[c(TRUE,FALSE)]),
                   long2 = party2)
  
  df$date <- format(as.Date(df$date, "%m/%d/%y"), "%m-%d-19%y") 
  df$year <- format(as.Date(df$date, "%m-%d-%Y"), "%Y")  
  df
  
}


ccFun <- function(x){
  
  custom_match <- c(`Yugoslavia` = "YUG",
                    `German Democratic Republic` = "DDR",
                    `South Yemen` = "YDYE",
                    `Vietnam, Republic of (South)` = "SVM",
                    `Czechoslovakia` = "CSK") # NB: Tanganyika out of existence before 1970
  
  x$state1 <- countrycode(x$state1, "country.name", "iso3c", custom_match = custom_match)  # remaining NAs are nonstate actors
  x$state2 <- countrycode(x$state2, "country.name", "iso3c", custom_match = custom_match)
  x <- na.omit(x)
  x
  
}





# --- Import CSVs extracted from WTI database --- #

# --- Science:
science <- read_csv("data/raw/wti/7scien_wti.csv") #From 7SCIEN
science.df <- fix.WTI(science)
sci.sub <- subset(science.df, year >= 1955 & year <= 1990)
sci.el <- ccFun(sci.sub)



# --- Military:
mil <- read_csv("data/raw/wti/9milit_wti.csv") #From 9MILIT
mil.df <- fix.WTI(mil)
mil.sub <- subset(mil.df, year >= 1955 & year <= 1990)
mil.el <- ccFun(mil.sub)


# --- Commodities:
com <- read_csv("data/raw/wti/3commo_wti.csv") #3COMMO
com.df <- fix.WTI(com)
com.sub <- subset(com.df, year >= 1955 & year <= 1990)
com.el <- ccFun(com.sub)


# --- Fisheries:
fish <- read_csv("data/raw/wti/8fish_wti.csv") #8FISH
fish.df <- fix.WTI(fish)
fish.sub <- subset(fish.df, year >= 1955 & year <= 1990)
fish.el <- ccFun(fish.sub)


# --- Telecomms:
telco <- read_csv("data/raw/wti/6telco_wti.csv") #6TELCO
telco.df <- fix.WTI(telco)
telco.sub <- subset(telco.df, year >= 1955 & year <= 1990)
telco.el <- ccFun(telco.sub)
















# --- Master node set --- #

nodes_master <- unique(c(sci.el$state1, sci.el$state2,
                         mil.el$state1, mil.el$state2,
                         com.el$state1, com.el$state2,
                         fish.el$state1, fish.el$state2,
                         telco.el$state1, telco.el$state2))

mat_master <- matrix(0, ncol = length(nodes_master), nrow = length(nodes_master),
                     dimnames = list(nodes_master, nodes_master))


# --- Science:

sci.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(sci.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), mat_master, remove=T, add=T, value=0)
  sci.list[[i]] <- n2
}



# --- Military:

mil.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(mil.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), mat_master, remove=T, add=T, value=0)
  mil.list[[i]] <- n2
}



# --- Commodities:

com.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(com.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), mat_master, remove=T, add=T, value=0)
  com.list[[i]] <- n2
}



# --- Fisheries:

fish.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(fish.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), mat_master, remove=T, add=T, value=0)
  fish.list[[i]] <- n2
}


# --- Telecomms:

telco.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(telco.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), mat_master, remove=T, add=T, value=0) 
  telco.list[[i]] <- n2
}



saveRDS(sci.list,  file = "data/comm_detection/candidate_layers/strong/sci_masternodes.rds")
saveRDS(mil.list,  file = "data/comm_detection/candidate_layers/strong/mil_masternodes.rds")
saveRDS(com.list,  file = "data/comm_detection/candidate_layers/strong/com_masternodes.rds")
saveRDS(fish.list, file = "data/comm_detection/candidate_layers/strong/fish_masternodes.rds")
saveRDS(telco.list, file = "data/comm_detection/candidate_layers/strong/telco_masternodes.rds")









# --- Weak signal node set --- #

vo_cands <- readRDS("data/comm_detection/candidate_layers/weak/vo_cands.rds")  # names of vo_cands = sp_cands 


# --- Science:

sci.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(sci.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), vo_cands[[i]], remove=T, add=T, value=0)
  sci.list[[i]] <- n2
}



# --- Military:

mil.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(mil.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), vo_cands[[i]], remove=T, add=T, value=0)
  mil.list[[i]] <- n2
}



# --- Commodities:

com.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(com.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), vo_cands[[i]], remove=T, add=T, value=0)
  com.list[[i]] <- n2
}



# --- Fisheries:

fish.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(fish.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), vo_cands[[i]], remove=T, add=T, value=0)
  fish.list[[i]] <- n2
}


# --- Telecomms:

telco.list <- list()
for(i in 1970:1990){
  j <- i - 9
  s <- subset(telco.el, as.numeric(year)>=j & as.numeric(year)<=i) 
  n <- network(s, matrix.type="edgelist", directed=F) 
  n2 <- adjust(as.matrix.network(n), vo_cands[[i]], remove=T, add=T, value=0)
  telco.list[[i]] <- n2
}




saveRDS(sci.list,  file = "data/comm_detection/candidate_layers/strong/sci_weaknodes.rds")
saveRDS(mil.list,  file = "data/comm_detection/candidate_layers/strong/mil_weaknodes.rds")
saveRDS(com.list,  file = "data/comm_detection/candidate_layers/strong/com_weaknodes.rds")
saveRDS(fish.list, file = "data/comm_detection/candidate_layers/strong/fish_weaknodes.rds")
saveRDS(telco.list, file = "data/comm_detection/candidate_layers/strong/telco_weaknodes.rds")

